/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.sms.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.modules.sms.dao.SmsSendQueueDao;
import com.chlitina.store.modules.sms.entity.SmsSendQueue;
import com.chlitina.store.modules.sms.utils.SmsUtils;

/**
 * 短信Service
 * @author lvbo
 * @version 2013-06-21
 */
@Component
@Transactional(readOnly = true)
public class SmsSendQueueService extends BaseService {

	private static Logger logger = LoggerFactory.getLogger(SmsSendQueueService.class);
	
	@Autowired
	private SmsSendQueueDao smsSendQueueDao;
	
	public SmsSendQueue get(Long id) {
		return smsSendQueueDao.findOne(id);
	}
	
	public List<SmsSendQueue> getOneHour(Date time) {
		DetachedCriteria dc = smsSendQueueDao.createDetachedCriteria();
		dc.add(Restrictions.between("planSendTime", DateUtils.getBeginOfDay(time), DateUtils.getEndOfDay(time)));
		dc.add(Restrictions.eq("status", SmsSendQueue.WAIT_STATUS));
		List<SmsSendQueue> list = smsSendQueueDao.find(dc);
		for (SmsSendQueue smsSendQueue : list) {
			smsSendQueue.setStatus(0);
		}
		smsSendQueueDao.save(list);
		smsSendQueueDao.flush();
		return list;
	}
	
	public Page<SmsSendQueue> find(Page<SmsSendQueue> page, SmsSendQueue smsSendQueue) {
		DetachedCriteria dc = smsSendQueueDao.createDetachedCriteria();
		dc.addOrder(Order.desc("id"));
		return smsSendQueueDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public List<SmsSendQueue> findByParameter(String start, String end) {
		Date startDate = null;
		Date endDate = null;
		try {
			startDate = DateUtils.parseDate(start);
			startDate = DateUtils.getBeginOfDay(startDate);
			endDate = DateUtils.parseDate(end);
			endDate = DateUtils.getEndOfDay(endDate);
		} catch (Exception e) {
			logger.debug("日期转换异常");
		}
		return smsSendQueueDao.findByParameter(startDate, endDate);
	}
	
	@Transactional(readOnly = false)
	public Long countByParameter(String start, String end) {
		Date startDate = null;
		Date endDate = null;
		try {
			startDate = DateUtils.parseDate(start);
			startDate = DateUtils.getBeginOfDay(startDate);
			endDate = DateUtils.parseDate(end);
			endDate = DateUtils.getEndOfDay(endDate);
		} catch (Exception e) {
			logger.debug("日期转换异常");
		}
		return smsSendQueueDao.countByParameter(SmsUtils.default_length, startDate, endDate);
	}
	
	@Transactional(readOnly = false)
	public void save(SmsSendQueue smsSendQueue) {
		smsSendQueueDao.save(smsSendQueue);
	}
}
